﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Business;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Business;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class ProductInStoreOrderEditControl : BaseEditControl {

        private FlexBindingSource<ProductInStoreOrder> _fbs;
        private ProduceConductProcessor _produceConductProcessor;
        private OutsourceOrderProcessor _outsourceOrderProcessor;

        public override object BaseEditModel {
            get {
                return this._fbs.CurrentItem;
            }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<ProductInStoreOrder>(); }
                this._fbs.CurrentItem = (ProductInStoreOrder)value;
            }
        }

        public ProductInStoreOrderEditControl() {
            this.InitializeComponent();
        }

        private void BindStoreBasis() {
            StoreBasisProcessor storeBasisProcessor = new StoreBasisProcessor();
            List<StoreBasisViewModel> storeBasisList = storeBasisProcessor.FetchStoreBasis().Where(x => x.IsEnabled).ToList();

            this.ddlStoreBasis.DisplayMemberEx = "Code";
            this.ddlStoreBasis.ValueMemberEx = "Key";
            this.ddlStoreBasis.HeightEx = 300;
            this.ddlStoreBasis.WidthEx = 400;
            this.ddlStoreBasis.PageSizeEx = 20;
            this.ddlStoreBasis.DataBind(new StoreBasisViewModel(), x => new { x.Code, x.Name, x.StoreType }, storeBasisList);
        }

        private void BindProduceConduct() {
            ProduceConductSearchModel searchModel = new ProduceConductSearchModel();
            ProduceConductProcessor produceConductProcessor = new ProduceConductProcessor();
            List<ProduceConductViewModel> produceConductList = produceConductProcessor.FetchProduceConductViewModelList(searchModel);

            this.ddlProduceConduct.DisplayMemberEx = "ProduceConductCode";
            this.ddlProduceConduct.ValueMemberEx = "Key";
            this.ddlProduceConduct.HeightEx = 300;
            this.ddlProduceConduct.WidthEx = 400;
            this.ddlProduceConduct.PageSizeEx = 20;
            this.ddlProduceConduct.DataBind(new ProduceConductViewModel(), x => new {
                x.ProduceConductCode,
                x.CustomerOrderCode,
                x.BatchAmount
            }, produceConductList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.ddlProduceConduct, x => x.SelectedValueEx, y => y.ProduceConductId);
            this._fbs.CreateBinding(this.ddlStoreBasis, x => x.SelectedValueEx, y => y.StoreBasisId);
            this._fbs.CreateBinding(this.cmbProductInStoreType, x => x.SelectedValue, y => y.InStoreTypeId);
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this._fbs.CreateBinding(this.tbRemarks, x => x.Text, y => y.Remarks);
            this._fbs.CreateBinding(this.cmbProduceUnit, x => x.SelectedValue, y => y.ProduceUnitId);
            this._fbs.CreateBinding(this.tbReceiptCode, x => x.Text, y => y.ReceiptCode);
            this._fbs.CreateBinding(this.tbQuantity, x => x.Text, y => y.Quantity, z => z.ToString("G0"));
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.ddlProduceConduct, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlProduceConduct, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlStoreBasis, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlStoreBasis, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbProductInStoreType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbProductInStoreType, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbProduceUnit, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbProduceUnit, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbQuantity, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbQuantity, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbQuantity, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbQuantity, "数量只能由数字组成");
        }

        private void ProductInStoreEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<ProductInStoreOrder>(); }
            this._produceConductProcessor = new ProduceConductProcessor();
            this._outsourceOrderProcessor = new OutsourceOrderProcessor();

            this.BindBasics(this.cmbProductInStoreType, BasicsType.ProductInStoreType);
            this.BindBasics(this.cmbProduceUnit, BasicsType.ProduceUnit);
            this.BindProduceConduct();
            this.BindStoreBasis();

            this.CreateFormBindings();
            this.BindValidation();

            this.ddlProduceConduct.Enabled = false;
            if (this._fbs.CurrentItem.ClassStatus == ClassStatus.Added) {
                string autoCode = CommonHelper.GenerateAutoCode("PI");
                int maxNumber = new ProductInStoreOrderProcessor().GetTheMaxFlowNumber(autoCode);
                this.tbCode.Text = this._fbs.CurrentItem.Code = CommonHelper.GendrateSpecificCode(autoCode, maxNumber);
                this.ddlProduceConduct.Enabled = true;
            }
        }

        private void ddlProductInStoreOrder_ValueChanged(object sender, EventArgs e) {
            if (this.ddlProduceConduct.SelectedValueEx == null) { return; }
            if (this._fbs.CurrentItem.ClassStatus == ClassStatus.Updated) { return; }
            int produceConductId = Convert.ToInt32(this.ddlProduceConduct.SelectedValueEx);
            ProduceConduct produceConduct = this._produceConductProcessor.FetchProduceConductById(produceConductId);
            OutsourceOrder outsourceOrder = this._outsourceOrderProcessor.FetchOutsourceOrderByProductConductId(produceConductId);
            this._fbs.CurrentItem.ProductId = produceConduct.ProductId;
            this._fbs.CurrentItem.VendorId = null;
            this._fbs.CurrentItem.IsOutsource = false;
            this.lblBatchAmount.Text = produceConduct.BatchAmount.ToString("G0");
            this.tlpEdit.Controls.RemoveByKey("lblOutsourceVendor");
            this.cmbProduceUnit.Visible = true;

            if (outsourceOrder != null) {
                Label lblOutsourceVendor = new Label();
                lblOutsourceVendor.Name = "lblOutsourceVendor";
                lblOutsourceVendor.Text = outsourceOrder.Vendor.Code;
                lblOutsourceVendor.Dock = DockStyle.Fill;
                lblOutsourceVendor.TextAlign = ContentAlignment.MiddleLeft;
                this.tlpEdit.Controls.Add(lblOutsourceVendor);
                this.tlpEdit.SetRow(lblOutsourceVendor, 2);
                this.tlpEdit.SetColumn(lblOutsourceVendor, 1);
                this.cmbProduceUnit.Visible = false;
                this._fbs.CurrentItem.VendorId = outsourceOrder.VendorId;
                this._fbs.CurrentItem.IsOutsource = true;
                this.ValidatorForEdit.RemoveValidationInfo(this.cmbProduceUnit);
            }
        }
    }
}
